package weaver.interfaces.zhlx.duss.action.financemodule.ec;

import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-08-18
 * @Description: 资金计划增补审批流程计划ID自动生成，规则：年月+部门编码+流水号(不能有重复)（202308040001）
 * @ModifyLog:
 **/
public class WeaZjjhzblcCreatejhidAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        writeLog(">>>>>WeaZjjhzblcCreatejhidAction>>>>>requestid=" + requestid);
        try {
            RecordSet rs = new RecordSet();
            rs.executeQuery("select id,sqrq from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            String sqrq = Util.null2String(rs.getString("sqrq"));
            rs.executeQuery("select id,jhid from " + tablename + "_dt1 where mainid=?", mainid);
            while (rs.next()) {
                String id = Util.null2String(rs.getString("id"));
                String jhid = Util.null2String(rs.getString("jhid"));
                if ("".equals(jhid) && !"".equals(requestid)) {
                    String maxjhid = getMaxSerialNumber(tablename, mainid,requestid);
                    if (!"".equals(maxjhid)) {
                        String maxlsh = maxjhid.substring(maxjhid.length() - 4);
                        String ny = sqrq.replace("-", "").substring(0, 6);
                        String newjhid = "ZB"+ny + requestid + autoGenericCode(maxlsh,4);
                        rs.executeUpdate("update " + tablename + "_dt1 set jhid=? where id=?", newjhid, id);
                    } else {
                        String ny = sqrq.replace("-", "").substring(0, 6);
                        String newjhid = "ZB"+ny + requestid + "0001";
                        rs.executeUpdate("update " + tablename + "_dt1 set jhid=? where id=?", newjhid, id);
                    }
                }
            }

            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaZjjhtbsplcCreatejhidAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

    public String getMaxSerialNumber(String tablename, String mainid,String requestid) {
        RecordSet rs = new RecordSet();
        String sql = "select max(jhid) from " + tablename + "_dt1 where mainid=? and jhid like '%"+requestid+"%'";
        rs.executeQuery(sql,mainid);
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    private static String autoGenericCode(String code, int num) {
        String result = "";
        result = String.format("%0" + num + "d", Integer.parseInt(code) + 1);
        return result;
    }

}